package com.jack.lib.core.init

import android.app.Application
import android.content.Context
import android.graphics.Rect
import androidx.startup.Initializer
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ScreenUtils
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.ConsolePrinter
import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.backup.NeverBackupStrategy
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy
import com.hjq.language.MultiLanguages
import com.jack.lib.core.log.LogFlattener
import com.jack.lib.core.log.LogWriter
import com.jack.lib.core.log.LogFileNameGenerator
import com.jack.lib.core.ui.JToast
import com.jack.lib.core.utils.DeviceInfo
import com.jack.lib.core.utils.TimeUtils
import com.kongzue.dialog.util.DialogSettings
import com.tencent.mmkv.MMKV
import java.util.*


/**
 * author : chong.huang
 * create : 2021/12/31
 * desc   :
 */
class AppInitializer : Initializer<Any> {
    override fun create(context: Context): Any {
        if (context is Application) {
            init(context)
        }
        return "null"
    }

    override fun dependencies(): MutableList<Class<out Initializer<*>>> {
        return Collections.emptyList()
    }

    private fun init(app: Application) {

        JToast.init(app)

        MMKV.initialize(app);

        MultiLanguages.init(app)

        initDialog()

        initLogger(app)

        initDevice()
    }

    private fun initDialog() {
        DialogSettings.theme = (DialogSettings.THEME.LIGHT);
        DialogSettings.tipTheme = (DialogSettings.THEME.DARK);
        DialogSettings.style = (DialogSettings.STYLE.STYLE_MIUI)
    }

    private fun initLogger(app: Application) {

        val config = LogConfiguration.Builder()
            .logLevel(LogLevel.ALL)
            .tag("jlog")
            .disableThreadInfo()
            .disableStackTrace()
            .build()

        val androidPrinter = AndroidPrinter(true) // 通过 android.util.Log 打印日志的打印器

        val filePrinter = FilePrinter.Builder(app.filesDir.toString() + "/log/") // 指定保存日志文件的路径
            .fileNameGenerator(LogFileNameGenerator()) // 指定日志文件名生成器，默认为 ChangelessFileNameGenerator("log")
            .backupStrategy(NeverBackupStrategy()) // 指定日志文件备份策略，默认为 FileSizeBackupStrategy(1024 * 1024)
            .cleanStrategy(FileLastModifiedCleanStrategy(TimeUtils.MONTH_MILLIS)) // 指定日志文件清除策略，默认为 NeverCleanStrategy()
            .flattener(LogFlattener())
            .writer(LogWriter())
            .build()

        XLog.init(config, androidPrinter, filePrinter);

        LogUtils.getConfig().isLog2FileSwitch = true
        LogUtils.getConfig().saveDays = 30
        LogUtils.getConfig().globalTag = "jlog"
        LogUtils.getConfig().setBorderSwitch(false)
        LogUtils.getConfig().isLogHeadSwitch = false
        LogUtils.getConfig().stackDeep = 1
        LogUtils.getConfig().stackOffset = 0
    }

    private fun initDevice() {
        DeviceInfo.sScreenRect = Rect(0, 0, ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight())
        DeviceInfo.sAutoScaleX = DeviceInfo.sScreenRect.width() * 1.0f / DeviceInfo.UI_WIDTH
        DeviceInfo.sAutoScaleY = DeviceInfo.sScreenRect.height() * 1.0f / DeviceInfo.UI_HEIGHT
    }

}